package com.yoosora.plugins;

import cn.hutool.json.JSONObject;
import com.mikuac.shiro.annotation.MessageHandler;
import com.mikuac.shiro.common.utils.MsgUtils;
import com.mikuac.shiro.common.utils.RegexUtils;
import com.mikuac.shiro.core.Bot;
import com.mikuac.shiro.core.BotPlugin;
import com.mikuac.shiro.dto.event.message.WholeMessageEvent;
import com.yoosora.common.consts.ConstsS;
import com.yoosora.utils.RegexConst;
import com.yoosora.utils.Strings;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.validation.constraints.NotNull;


@Slf4j
@Component
public class OilPricePlugin extends BotPlugin {
	@Autowired
	private RedisTemplate<String, String> redisTemplate;
	//后续加入常量类	
	private final static String JUHE_OIL_PRE="JUHE_OIL_";
	private static StringBuilder sb = new StringBuilder();
	
	@MessageHandler(cmd = RegexConst.JUHE_OIL)
	
	public void oilMsg(@NotNull Bot bot, @NotNull WholeMessageEvent event){
		String msg = event.getMessage();
			String area = RegexUtils.regexGroup(RegexConst.JUHE_OIL,msg , 1);
			String sendMsg=Strings.EMPTY;
			if(Strings.isNotEmpty(area)){
				log.info(area);
				String oilPriceStr = redisTemplate.opsForValue().get(JUHE_OIL_PRE + area);
				if(Strings.isNotEmpty(oilPriceStr)){
					JSONObject oilPrice = new JSONObject(oilPriceStr);
					sb.append(area).append("地区油价(元/升):").append(ConstsS.RN)
							.append("98号汽油: ").append(oilPrice.get("98h")).append(ConstsS.RN)
							.append("95号汽油: ").append(oilPrice.get("95h")).append(ConstsS.RN)
							.append("92号汽油: ").append(oilPrice.get("92h")).append(ConstsS.RN)
							.append("0号柴油: ").append(oilPrice.get("0h")).append(ConstsS.RN);
					sendMsg = sb.toString();
					sb.setLength(0);
				}else{
					sendMsg = "您输入的地区无油价信息";
				}
			}else{
				sendMsg="请用油价查询#地区的格式进行查询";
			}
			bot.sendMsg(event, MsgUtils.builder().text(sendMsg).build(), false);
	}
	
	/*@Override
	public int onGroupMessage(@NotNull Bot bot, @NotNull GroupMessageEvent event) {
		String msg = event.getMessage();
		if(msg.matches(RegexConst.JUHE_OIL)){
			long groupId = event.getGroupId();
			long userId = event.getUserId();
			String area = RegexUtils.regexGroup(RegexConst.JUHE_OIL,msg , 1);
			String sendMsg=Strings.EMPTY;
			if(Strings.isNotEmpty(area)){
				log.info(area);
				String oilPriceStr = redisTemplate.opsForValue().get(JUHE_OIL_PRE + area);
				if(Strings.isNotEmpty(oilPriceStr)){
//					log.info(oilPriceStr);
					JSONObject oilPrice = new JSONObject(oilPriceStr);
//					log.info(oilPriceStr);
					sb.append(area).append("地区油价(元/升):").append(ConstsS.RN)
							.append("98号汽油: ").append(oilPrice.get("98h")).append(ConstsS.RN)
							.append("95号汽油: ").append(oilPrice.get("95h")).append(ConstsS.RN)
							.append("92号汽油: ").append(oilPrice.get("92h")).append(ConstsS.RN)
							.append("0号柴油: ").append(oilPrice.get("0h")).append(ConstsS.RN);
					sendMsg = sb.toString();
					sb.setLength(0);
				}else{
					sendMsg = "您输入的地区无油价信息";
				}
			}else{
				sendMsg="请用油价查询#地区的格式进行查询";
			}
			bot.sendGroupMsg(groupId, MsgUtils.builder().text(sendMsg).build(), false);
		}
		return MESSAGE_IGNORE;
	}*/
}
